/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.policy.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.annotation.rest.AnonymousGetMapping;
import com.fzhucc.modules.policy.domain.Policy;
import com.fzhucc.modules.policy.domain.dto.PolicyDto;
import com.fzhucc.modules.policy.service.PolicyService;
import com.fzhucc.modules.policy.domain.dto.PolicyQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2023-02-27
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "政策库:管理")
@RequestMapping("/api/policy")
public class PolicyController {

    private final PolicyService policyService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('policy:list')")
    public void exportPolicy(HttpServletResponse response, PolicyQueryCriteria criteria) throws IOException {
        criteria.setIsDelete(0);
        policyService.download(policyService.queryAll(criteria), response);
    }

    @Log("查询政策库")
    @ApiOperation("查询政策库")
    @AnonymousGetMapping("/show")
    /*@PreAuthorize("@el.check('policy:list')")*/
    public ResponseEntity<Object> queryPolicy(PolicyQueryCriteria criteria, Pageable pageable){
        criteria.setIsDelete(0);
        return new ResponseEntity<>(policyService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增政策库")
    @ApiOperation("新增政策库")
    @PreAuthorize("@el.check('policy:add')")
    public ResponseEntity<Object> createPolicy(@Validated @RequestBody Policy resources){
        return new ResponseEntity<>(policyService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改政策库")
    @ApiOperation("修改政策库")
    @PreAuthorize("@el.check('policy:edit')")
    public ResponseEntity<Object> updatePolicy(@Validated @RequestBody Policy resources){
        policyService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除政策库")
    @ApiOperation("删除政策库")
    @PreAuthorize("@el.check('policy:del')")
    public ResponseEntity<Object> deletePolicy(@RequestBody Integer[] ids) {
        policyService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/{id}")
    @Log("查询单个政策")
    @ApiOperation("查询单个政策")
    public PolicyDto queryPolicyDtoById(@PathVariable Integer id) {
        return policyService.findById(id);
    }
}